package com.evaluationSystem.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.evaluationSystem.config.PropertiesConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName LogOperationUtil
 * @Description TODO
 * @Author yinzhenxing
 * @Date 2022/11/20 14:46
 **/
@Component
@Slf4j
public class LogOperationService {
    @Autowired
    private PropertiesConfig propertiesConfig;

    public void saveLog(LogOperationParams logOperationParams) {
        // 请求参数
        if (!"dev".equals(propertiesConfig.getEnv())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appId", propertiesConfig.getAppId());
            jsonObject.put("appName", "渠道扣罚直达平台系统");
            jsonObject.put("operateId", propertiesConfig.getAppId() + "_" + logOperationParams.getOperateId());
            // 1菜单（登陆）/2按钮/3链接/4其它
            jsonObject.put("operateType", logOperationParams.getOperateType());
            jsonObject.put("operateName", logOperationParams.getOperateName());
            // add delete update save goto
            jsonObject.put("buttonType", logOperationParams.getButtonType());
            jsonObject.put("regionCode", "E");
            jsonObject.put("staffCode", logOperationParams.getStaffCode());
            jsonObject.put("staffName", logOperationParams.getStaffName());
            jsonObject.put("isBisp", "Y");

            String toJSONString = jsonObject.toString();

            String post = HttpUtil.post(propertiesConfig.getLogUrl(), toJSONString, 5000);

            JSONObject response = JSONObject.parseObject(post);

            Integer code = response.getInteger("code");
            if (code == 0) {
                log.info("操作日志记录接口能力调用成功，请求地址：{}，请求参数：{}，返回信息：{}", propertiesConfig.getLogUrl(), toJSONString, response);
            } else {
                log.error("操作日志记录接口能力调用失败，请求地址：{}，请求参数：{}，返回信息：{}", propertiesConfig.getLogUrl(), toJSONString, response);
            }

        }
    }
}
